package ajax;

import javax.security.auth.login.LoginException;

public enum MessagesAjaxActions {

    LOGIN_SEARCH {

        @Override
        public void doAction(MessagesAjaxBean bean) throws LoginException {
            String login = bean.getStringFromParam("login").trim();

            if (login == null || login.isEmpty()) {
                throw new LoginException("Error");
            }
            String message = null;
            if (login.equals("login")) {
                message = "used";
            } else {
                message = "free";
            }
            bean.addPropertyToRes("userlogin", message);
        }
    },
    
    EMAIL_SEARCH 

    {
        @Override
        public void doAction
        (MessagesAjaxBean bean
        ) throws LoginException  {
            String email = bean.getStringFromParam("email").trim();

            if (email == null || email.isEmpty()) {
                throw new LoginException("Error");
            }
            String message = null;
            if (email.equals("email")) {
                message = "used";
            } else {
                message = "free";
            }
            bean.addPropertyToRes("userEmail", message);
        }
    };	
        
	public static MessagesAjaxActions getActionFromString(String str) throws LoginException {
        if (str != null && !str.isEmpty()) {
            if (str.equals("login_search")) {
                return LOGIN_SEARCH;
            }
            if (str.equals("email_search")) {
                return EMAIL_SEARCH;
            }
        }
        throw new LoginException("Error");
        
    }

    public abstract void doAction(MessagesAjaxBean bean) throws LoginException;
}
